import React, { Component } from 'react';
import {  NavBar,Button,Toast} from 'antd-mobile'
import "./login.css"
import service from '../../api';

class login extends Component {
    constructor(props){
        super(props)
        this.state = {
            loginname:'',
            password:''
        }
    }
    onChange(e){
        this.setState({
            [e.target.name] : e.target.type == 'checkbox' ?  e.target.checked  : e.target.value 
        })
    }
    async toLogin(){
       var res = await service.user.user_login({
        loginname:this.props.location.state.tel,
        password:this.state.password
        })
        if(res.data.code == 200){
            this.props.history.push('/index/home')
            Toast.show({content:'登录成功！'})
        }else{
            Toast.show({content:'登录失败！'})
        }

    }
    render() {
        return (
            <div className="login">
                <NavBar onBack={()=>{this.back()}}>登录页</NavBar>
                <div className="block">
                    <input name='phone' type="text" placeholder='请输入手机号' value={this.state.phone || ''} onChange={(e)=>{ this.onChange(e) }}/>
                </div>
                <div className="block">
                    <input name='pass' type="password" placeholder='请输入密码' value={this.state.pass || ''} onChange={(e)=>{ this.onChange(e) }}/>
                </div>
                <button onClick={()=>{this.toLogin()}}>登录</button>
                <span className='noid' onClick={()=>{this.register()}}>没有账号，去注册......</span>
            </div>
        );
    }
    async back(){
        this.props.history.go(-1);
    }
    async register(){
        this.props.history.push('/register/phone');
    }
   
}

export default login;